﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:BType
    /// </summary>
    public partial class BType
    {
        public BType()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("BtypeFID", "BType");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int BtypeFID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from BType");
            strSql.Append(" where BtypeFID=@BtypeFID");
            var parameters = new SqlParameter[] { new SqlParameter("@BtypeFID", SqlDbType.Int, 4)
            };
            parameters[0].Value = BtypeFID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.BType model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into BType(");
            strSql.Append("BtypeID,Parid,Leveal,Bsonnum,Soncount,Busercode,Bfullname,BName,BNamePY,Area,TelAddress,PostCode,Person,BComment,Fax,EMail,IsStop,moPhone,Linker,LinkerTel,DTypeID)");
            strSql.Append(" values (");
            strSql.Append("@BtypeID,@Parid,@Leveal,@Bsonnum,@Soncount,@Busercode,@Bfullname,@BName,@BNamePY,@Area,@TelAddress,@PostCode,@Person,@BComment,@Fax,@EMail,@IsStop,@moPhone,@Linker,@LinkerTel,@DTypeID)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@BtypeID", SqlDbType.VarChar, 25),
                    new SqlParameter("@Parid", SqlDbType.VarChar, 25),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@Bsonnum", SqlDbType.Int, 4),
                    new SqlParameter("@Soncount", SqlDbType.Int, 4),
                    new SqlParameter("@Busercode", SqlDbType.VarChar, 100),
                    new SqlParameter("@Bfullname", SqlDbType.VarChar, 200),
                    new SqlParameter("@BName", SqlDbType.VarChar, 100),
                    new SqlParameter("@BNamePY", SqlDbType.VarChar, 100),
                    new SqlParameter("@Area", SqlDbType.VarChar, 1024),
                    new SqlParameter("@TelAddress", SqlDbType.VarChar, 50),
                    new SqlParameter("@PostCode", SqlDbType.VarChar, 20),
                    new SqlParameter("@Person", SqlDbType.VarChar, 50),
                    new SqlParameter("@BComment", SqlDbType.VarChar, 256),
                    new SqlParameter("@Fax", SqlDbType.VarChar, 30),
                    new SqlParameter("@EMail", SqlDbType.VarChar, 50),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@moPhone", SqlDbType.VarChar, 50),
                    new SqlParameter("@Linker", SqlDbType.VarChar, 50),
                    new SqlParameter("@LinkerTel", SqlDbType.VarChar, 50),
                    new SqlParameter("@DTypeID", SqlDbType.VarChar, 50) };
            parameters[0].Value = model.BtypeID;
            parameters[1].Value = model.Parid;
            parameters[2].Value = model.Leveal;
            parameters[3].Value = model.Bsonnum;
            parameters[4].Value = model.Soncount;
            parameters[5].Value = model.Busercode;
            parameters[6].Value = model.Bfullname;
            parameters[7].Value = model.BName;
            parameters[8].Value = model.BNamePY;
            parameters[9].Value = model.Area;
            parameters[10].Value = model.TelAddress;
            parameters[11].Value = model.PostCode;
            parameters[12].Value = model.Person;
            parameters[13].Value = model.BComment;
            parameters[14].Value = model.Fax;
            parameters[15].Value = model.EMail;
            parameters[16].Value = model.IsStop;
            parameters[17].Value = model.moPhone;
            parameters[18].Value = model.Linker;
            parameters[19].Value = model.LinkerTel;
            parameters[20].Value = model.DTypeID;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.BType model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update BType set ");
            strSql.Append("BtypeID=@BtypeID,");
            strSql.Append("Parid=@Parid,");
            strSql.Append("Leveal=@Leveal,");
            strSql.Append("Bsonnum=@Bsonnum,");
            strSql.Append("Soncount=@Soncount,");
            strSql.Append("Busercode=@Busercode,");
            strSql.Append("Bfullname=@Bfullname,");
            strSql.Append("BName=@BName,");
            strSql.Append("BNamePY=@BNamePY,");
            strSql.Append("Area=@Area,");
            strSql.Append("TelAddress=@TelAddress,");
            strSql.Append("PostCode=@PostCode,");
            strSql.Append("Person=@Person,");
            strSql.Append("BComment=@BComment,");
            strSql.Append("Fax=@Fax,");
            strSql.Append("EMail=@EMail,");
            strSql.Append("IsStop=@IsStop,");
            strSql.Append("moPhone=@moPhone,");
            strSql.Append("Linker=@Linker,");
            strSql.Append("LinkerTel=@LinkerTel,");
            strSql.Append("DTypeID=@DTypeID");
            strSql.Append(" where BtypeFID=@BtypeFID");
            var parameters = new SqlParameter[] { new SqlParameter("@BtypeID", SqlDbType.VarChar, 25),
                    new SqlParameter("@Parid", SqlDbType.VarChar, 25),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@Bsonnum", SqlDbType.Int, 4),
                    new SqlParameter("@Soncount", SqlDbType.Int, 4),
                    new SqlParameter("@Busercode", SqlDbType.VarChar, 100),
                    new SqlParameter("@Bfullname", SqlDbType.VarChar, 200),
                    new SqlParameter("@BName", SqlDbType.VarChar, 100),
                    new SqlParameter("@BNamePY", SqlDbType.VarChar, 100),
                    new SqlParameter("@Area", SqlDbType.VarChar, 1024),
                    new SqlParameter("@TelAddress", SqlDbType.VarChar, 50),
                    new SqlParameter("@PostCode", SqlDbType.VarChar, 20),
                    new SqlParameter("@Person", SqlDbType.VarChar, 50),
                    new SqlParameter("@BComment", SqlDbType.VarChar, 256),
                    new SqlParameter("@Fax", SqlDbType.VarChar, 30),
                    new SqlParameter("@EMail", SqlDbType.VarChar, 50),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@moPhone", SqlDbType.VarChar, 50),
                    new SqlParameter("@Linker", SqlDbType.VarChar, 50),
                    new SqlParameter("@LinkerTel", SqlDbType.VarChar, 50),
                    new SqlParameter("@DTypeID", SqlDbType.VarChar, 50),
                    new SqlParameter("@BtypeFID", SqlDbType.Int, 4) };
            parameters[0].Value = model.BtypeID;
            parameters[1].Value = model.Parid;
            parameters[2].Value = model.Leveal;
            parameters[3].Value = model.Bsonnum;
            parameters[4].Value = model.Soncount;
            parameters[5].Value = model.Busercode;
            parameters[6].Value = model.Bfullname;
            parameters[7].Value = model.BName;
            parameters[8].Value = model.BNamePY;
            parameters[9].Value = model.Area;
            parameters[10].Value = model.TelAddress;
            parameters[11].Value = model.PostCode;
            parameters[12].Value = model.Person;
            parameters[13].Value = model.BComment;
            parameters[14].Value = model.Fax;
            parameters[15].Value = model.EMail;
            parameters[16].Value = model.IsStop;
            parameters[17].Value = model.moPhone;
            parameters[18].Value = model.Linker;
            parameters[19].Value = model.LinkerTel;
            parameters[20].Value = model.DTypeID;
            parameters[21].Value = model.BtypeFID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int BtypeFID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from BType ");
            strSql.Append(" where BtypeFID=@BtypeFID");
            var parameters = new SqlParameter[] { new SqlParameter("@BtypeFID", SqlDbType.Int, 4)
            };
            parameters[0].Value = BtypeFID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string BtypeFIDlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from BType ");
            strSql.Append(" where BtypeFID in (" + BtypeFIDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.BType GetModel(int BtypeFID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 BtypeFID,BtypeID,Parid,Leveal,Bsonnum,Soncount,Busercode,Bfullname,BName,BNamePY,Area,TelAddress,PostCode,Person,BComment,Fax,EMail,IsStop,moPhone,Linker,LinkerTel,DTypeID from BType ");
            strSql.Append(" where BtypeFID=@BtypeFID");
            var parameters = new SqlParameter[] { new SqlParameter("@BtypeFID", SqlDbType.Int, 4)
            };
            parameters[0].Value = BtypeFID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.BType DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.BType();
            if (row != null)
            {
                if (row["BtypeFID"] != null && row["BtypeFID"].ToString() != string.Empty)
                {
                    model.BtypeFID = int.Parse(row["BtypeFID"].ToString());
                }
                if (row["BtypeID"] != null)
                {
                    model.BtypeID = row["BtypeID"].ToString();
                }
                if (row["Parid"] != null)
                {
                    model.Parid = row["Parid"].ToString();
                }
                if (row["Leveal"] != null && row["Leveal"].ToString() != string.Empty)
                {
                    model.Leveal = int.Parse(row["Leveal"].ToString());
                }
                if (row["Bsonnum"] != null && row["Bsonnum"].ToString() != string.Empty)
                {
                    model.Bsonnum = int.Parse(row["Bsonnum"].ToString());
                }
                if (row["Soncount"] != null && row["Soncount"].ToString() != string.Empty)
                {
                    model.Soncount = int.Parse(row["Soncount"].ToString());
                }
                if (row["Busercode"] != null)
                {
                    model.Busercode = row["Busercode"].ToString();
                }
                if (row["Bfullname"] != null)
                {
                    model.Bfullname = row["Bfullname"].ToString();
                }
                if (row["BName"] != null)
                {
                    model.BName = row["BName"].ToString();
                }
                if (row["BNamePY"] != null)
                {
                    model.BNamePY = row["BNamePY"].ToString();
                }
                if (row["Area"] != null)
                {
                    model.Area = row["Area"].ToString();
                }
                if (row["TelAddress"] != null)
                {
                    model.TelAddress = row["TelAddress"].ToString();
                }
                if (row["PostCode"] != null)
                {
                    model.PostCode = row["PostCode"].ToString();
                }
                if (row["Person"] != null)
                {
                    model.Person = row["Person"].ToString();
                }
                if (row["BComment"] != null)
                {
                    model.BComment = row["BComment"].ToString();
                }
                if (row["Fax"] != null)
                {
                    model.Fax = row["Fax"].ToString();
                }
                if (row["EMail"] != null)
                {
                    model.EMail = row["EMail"].ToString();
                }
                if (row["IsStop"] != null && row["IsStop"].ToString() != string.Empty)
                {
                    model.IsStop = int.Parse(row["IsStop"].ToString());
                }
                if (row["moPhone"] != null)
                {
                    model.moPhone = row["moPhone"].ToString();
                }
                if (row["Linker"] != null)
                {
                    model.Linker = row["Linker"].ToString();
                }
                if (row["LinkerTel"] != null)
                {
                    model.LinkerTel = row["LinkerTel"].ToString();
                }
                if (row["DTypeID"] != null)
                {
                    model.DTypeID = row["DTypeID"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select BtypeFID,BtypeID,Parid,Leveal,Bsonnum,Soncount,Busercode,Bfullname,BName,BNamePY,Area,TelAddress,PostCode,Person,BComment,Fax,EMail,IsStop,moPhone,Linker,LinkerTel,DTypeID ");
            strSql.Append(" FROM BType ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" BtypeFID,BtypeID,Parid,Leveal,Bsonnum,Soncount,Busercode,Bfullname,BName,BNamePY,Area,TelAddress,PostCode,Person,BComment,Fax,EMail,IsStop,moPhone,Linker,LinkerTel,DTypeID ");
            strSql.Append(" FROM BType ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM BType ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.BtypeFID desc");
            }
            strSql.Append(")AS Row, T.*  from BType T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

